basket.require({
	url : 'http://cdn.bootcss.com/jquery/2.1.4/jquery.min.js'
}, {
	url : "http://cdn.bootcss.com/angular.js/1.4.1/angular.min.js"
}, {
	url : "http://cdn.bootcss.com/alertify.js/0.3.11/alertify.min.js"
}, {
	url : settings.getIp() + "/peiwan-server/static/js/persistence.js"
}, {
	url : settings.getIp() + "/peiwan-server/static/js/timer.js"
}).then(ready);

function ready() {
	persistence.setNamespace("peiwan_admin");
	var peiwanAdmin = angular.module('peiwan_admin', []);
	peiwanAdmin.controller('CashRequestListCtrl', [ "$scope", function(s) {
		var offset = 0;
		var LIMIT = 10;
		if (!persistence.get("session")) {
			location.href = "admin_login.html";
			return;
		}
		var queryList = function(status) {
			params = {
				offset : offset,
				limit : LIMIT
			};
			if (status != null) {
				params.status = status;
			}
			ajax("/findCashRequest", params, function(data) {
				var json = JSON.parse(data);
				var cashRequests = json.entity;
				s.$apply(function() {
					s.cashRequests = cashRequests;
				});
			});
		}
		queryList(null);
		s.updateCashRequestStatus = function(id, status) {
			ajax("/updateCashRequestStatus", {
				session : persistence.get("session"),
				id : id,
				status : status
			}, function(data) {
				var json = JSON.parse(data);
				console.log(data);
				if (json.status == 1) {
					location.href = "admin_login.html";
					return;
				}
				queryList(null);
			});
		}
		s.prePage = function() {
			if (offset >= LIMIT) {
				offset -= LIMIT;
				var page = parseInt($("#page_input").val());
				$("#page_input").val(--page + "");
				queryList();
			}

		}
		s.toPage = function() {
			var page = parseInt($("#page_input").val());
			offset = (page - 1) * LIMIT;
			queryList();
		}
		s.nextPage = function() {
			offset += LIMIT;
			var page = parseInt($("#page_input").val());
			$("#page_input").val(++page + "");
			queryList();
		}
	} ]);
}